const mongoUtil = require("./mongo_util")
const collectionName = "students"
// 获取热门电视剧
function getData(value, callback) {
    const mmp = mongoUtil.getCollection(collectionName)
    mmp.then(collection => {
        if (Object.keys(value).length > 0) {
            collection.find({}).limit(Number(value.pagesize)).skip((Number(value.page) - 1) * Number(value.pagesize)).toArray(function (err, result) { // 返回集合中所有数据
                if (result.length) {
                    result.forEach((item) => {
                        item.classList.forEach((ite) => {
                            ite.studentList.forEach((it) => {
                                delete it.password
                                delete it.studentInfo
                                delete it.isStudentManage
                                // return {
                                //     "studentName": it.studentName,
                                //     "student_Number": it.student_Number,
                                //     "grade": it.grade
                                // }
                            })
                        })
                    })
                    console.log("gggggg===", result);
                    callback(null, result)
                    new Error()
                } else {
                    // console.log("hhhhhhhh");
                    callback(err ? err : {
                        reason: "没有数据"
                    }, result)
                }
            });
        } else {
            collection.find().toArray(function (err, result) { // 返回集合中所有数据
                if (result.length) {
                    
                    result.forEach((item) => {
                        item.classList.forEach((ite) => {
                            ite.studentList.forEach((it) => {
                                delete it.password
                                delete it.studentInfo
                                delete it.isStudentManage
                                // return {
                                //     "studentName": it.studentName,
                                //     "student_Number": it.student_Number,
                                //     "grade": it.grade
                                // }
                            })
                        })
                    })
                    console.log("gggggg", result);
                    callback(null, result)
                    new Error()
                } else {
                    // console.log("hhhhhhhh");
                    callback(err ? err : {
                        reason: "没有数据"
                    }, result)
                }
            });
        }

    })
}

module.exports = {
    getData
}